package com.lzm.flash;


import android.os.Bundle;

import java.util.Hashtable;
import java.util.Vector;

/**
 * Created by lzm on 2018/2/27.
 */

public class ChatMsgData implements Observer {

    INotifyChatViewUpdata m_iNotifyMsgActivity=null;
    MsgActivity    m_msgActivity=null;

    private  static  ChatMsgData m_instance=new ChatMsgData();

    private ChatMsgData(){}

    //获取类的实例
    public static  ChatMsgData getInstance()
    {
        return  m_instance;
    }

    //注册聊天界面的实例
    public void registerChatViewInstance(MsgActivity instance)
    {
        m_msgActivity=instance;
    }

    //注册更新更新聊天数据的显示接口

    public void registerIiNotifyChatUpdata(INotifyChatViewUpdata IChatView)
    {
        m_iNotifyMsgActivity=IChatView;
    }


    //获取聊天数据  不可能为空  但是如果该终端没有chat 则会获取到的 无数据 的vecetor
    Vector<CharMsg> getChatMsgData(String mac)
    {
        if (!m_chatData.containsKey(mac))
        {
            Vector<CharMsg> v=new Vector<>();
            m_chatData.put(mac,v);
        }
        return m_chatData.get(mac);
    }



    private class CharMsg
    {
        public  String msg;
        public  int type;

        CharMsg(String msg,int msg_type)
        {
            CharMsg.this.msg=msg;
            CharMsg.this.type=msg_type;
        }
    }
    Hashtable<String, Vector<CharMsg>> m_chatData;

    interface INotifyChatViewUpdata
    {
        public void chatMsgUpdata();
        public String getMac();
    }

    @Override
    public void updata(Handler handler) {
        switch (handler.getTag())
        {
            case Type.RECV_CHAT_MSG:
            {
                Bundle b=handler.getData();
                String mac=b.getString("mac");
                if (!m_chatData.containsKey(mac))
                {
                    //不存在该值
                    CharMsg chat=new CharMsg(
                            b.getString("msg"),
                            b.getInt("msg_type")
                    );
                    Vector<CharMsg> v=new Vector<>();
                    v.add(chat);
                    m_chatData.put(mac,v);
                }
                else
                {
                    //存在key
                    Vector<CharMsg> v=m_chatData.get(mac);
                    v.add(new CharMsg(b.getString("msg"),b.getInt("msg_type")));
                }
                //通知聊天界面更新数据
                if (m_iNotifyMsgActivity!=null)
                {
                    if (m_iNotifyMsgActivity.getMac()==mac)
                    {
                        m_iNotifyMsgActivity.chatMsgUpdata();
                    }
                }
            }
            break;
        }
    }
}






















